1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy.
4 : ressourcen/classes/archiver/class.zipvisitor.inc.php
5 : - Module group: File Manager
6 : - Description: Class "ZipVistitor"
7 : - Version: $Id: class.zipvisitor.inc.php 2348 2009-06-04 13:29:47Z commana $
8 : - Author(s): Bjoern Kasteleiner <bjoern.kasteleiner@mni.fh-giessen.de>
9 : +---------------------------------------------------------------------------+
10 : This program is free software; you can redistribute it and/or
11 : modify it under the terms of the GNU General Public License
12 : as published by the Free Software Foundation; either version 2
13 : of the License, or any later version.
14 : +---------------------------------------------------------------------------+
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 : +--------------------------------------------------------------------------*/
23 :
24 1 : if ( !defined("PATH_TO_ROOT") ) {
25 0 : define( "PATH_TO_ROOT", "../../../" );
26 0 : }
27 :
28 1 : require_once ("class.resource.inc.php");
29 1 : require_once ("class.file.inc.php");
30 1 : require_once ("class.folder.inc.php");
31 :
32 1 : require_once ("interface.resourcevisitor.inc.php");
33 :
34 : /**
35 : * Visitor implementation to move through Resource-Tree.
36 : *
37 : * @author Bjoern Kasteleiner <bjoern.kasteleiner@mni.fh-giessen.de>
38 : * @version $Id: class.zipvisitor.inc.php 2348 2009-06-04 13:29:47Z commana $
39 : */
40 1 : class ZipVisitor implements ResourceVisitor {
41 :
42 : private $recursion;
43 : private $archiver;
44 :
45 : /**
46 : * Constructor of ArchiveVisitor.
47 : *
48 : * Initialize Visitor with archive and recursion flag.
49 : *
50 : * @param unknown_type $archiver
51 : * @param unknown_type $recursion
52 : */
53 : public function __construct($archiver, $recursion) {
54 0 : assert(!is_null($archiver));
55 0 : assert($archiver instanceof ZipArchive);
56 :
57 0 : $this->archiver = $archiver;
58 0 : $this->recursion = $recursion;
59 0 : }
60 :
61 : /**
62 : * Setter for recursion flag.
63 : *
64 : * @param Boolean $recursion Recursion flag.
65 : */
66 : public function setRecursion($recursion) {
67 0 : $this->recursion = $recursion;
68 0 : }
69 :
70 : /**
71 : * Getter for recursion flag.
72 : *
73 : * @return Boolean Recursion flag
74 : */
75 : public function getRecursion() {
76 0 : return $this->recursion;
77 : }
78 :
79 : /**
80 : * Setter for archiver.
81 : *
82 : * @param unknown_type $archiver archiver
83 : */
84 : public function setArchiver($archiver) {
85 0 : $this->archiver = $archiver;
86 0 : }
87 :
88 : /**
89 : * Getter for archiver.
90 : *
91 : * @return unknown Archiver
92 : */
93 : public function getArchiver() {
94 0 : return $this->archiver;
95 : }
96 :
97 : /**
98 : * General visitor method for dipatching.
99 : *
100 : * Dispatch the visitor invokation.
101 : *
102 : * @param Resource $resource Resource
103 : * @param String $parentPath Parent path
104 : */
105 : public function visit(Resource $resource, $parentPath = "") {
106 0 : assert(!is_null($resource));
107 :
108 0 : if ($resource instanceof Folder) {
109 0 : $this->visitFolder($resource, $parentPath);
110 0 : } elseif ($resource instanceof File) {
111 0 : if ($resource->getType() == "file") {
112 0 : $this->visitFile($resource, $parentPath);
113 0 : } elseif ($resource->getType() == "link") {
114 0 : $this->visitLink($resource, $parentPath);
115 0 : } else {
116 0 : throw new ArchiverException("Unbekannter Typ des 'File'-Objekts");
117 : }
118 0 : }
119 0 : }
120 :
121 : /**
122 : * Internal visitor to handle Folder resources.
123 : *
124 : * @param Folder $folder Folder
125 : * @param String $parentPath Parent path
126 : */
127 : private function visitFolder(Folder $folder, $parentPath) {
128 0 : assert($folder instanceof Folder);
129 :
130 :
131 0 : $path = $parentPath.$folder->getLink();
132 :
133 0 : $suffix = 0;
134 0 : while ($this->archiver->locateName($path, ZipArchive::FL_NOCASE) != FALSE) {
135 0 : if ($suffix == 0) {
136 0 : $pattern = "$";
137 0 : } else {
138 0 : $pattern = "-".$suffix."$";
139 : }
140 0 : $suffix++;
141 0 : $replacement = "-".$suffix;
142 :
143 0 : $path = ereg_replace($pattern, $replacement, $filePath);
144 0 : }
145 0 : $path .= "/";
146 :
147 0 : if (!$this->archiver->addEmptyDir($path)) {
148 0 : throw new ArchiverException("Verzeichnis '".$folder->getName().
149 0 : "' konnte nicht zum Archiv hinzugefügt werden");
150 : }
151 :
152 0 : $this->addResourceDescription($folder, $path);
153 :
154 0 : $parentPath = $path;
155 :
156 0 : if ($this->recursion) {
157 0 : $subFolders = $folder->getSubFolders();
158 0 : foreach ($subFolders as $subFolder) {
159 0 : $subFolder->archiverAccept($this, $parentPath);
160 0 : }
161 0 : }
162 :
163 0 : $files = $folder->getFiles();
164 0 : foreach ($files as $file) {
165 0 : $file->archiverAccept($this, $parentPath);
166 0 : }
167 0 : }
168 :
169 : /**
170 : * Internal visitor to handle File resources.
171 : *
172 : * @param File $file File
173 : * @param String $parentPath Parent path
174 : */
175 : private function visitFile(File $file, $parentPath) {
176 0 : assert($file instanceof File);
177 :
178 0 : $filePath = $parentPath.$file->getOrigFileName();
179 :
180 0 : $suffix = 0;
181 0 : while ($this->archiver->locateName($filePath, ZipArchive::FL_NOCASE) != FALSE) {
182 0 : if ($suffix == 0) {
183 0 : $pattern = ".".$file->getExtension()."$";
184 0 : } else {
185 0 : $pattern = "-".$suffix.".".$file->getExtension()."$";
186 : }
187 0 : $suffix++;
188 0 : $replacement = "-".$suffix.".".$file->getExtension();
189 :
190 0 : $filePath = ereg_replace($pattern, $replacement, $filePath);
191 0 : }
192 :
193 0 : if (!$this->archiver->addFile($file->getPathToFile(), $filePath)) {
194 0 : throw new ArchiverException("Datei '".$file->getName().
195 0 : "' konnte nicht zum Archiv hinzugefügt werden");
196 : }
197 :
198 0 : self::addResourceDescription($file, $filePath);
199 0 : }
200 :
201 : /**
202 : * Internal visitor to handle Link resources.
203 : *
204 : * @param File $file File
205 : * @param String $parentPath Parent path
206 : */
207 : private function visitLink(File $file, $parentPath) {
208 0 : assert($file instanceof File);
209 :
210 0 : $filePath = $parentPath.$file->getName().".url";
211 :
212 0 : $content = "[InternetShortcut]";
213 0 : $content .= "\r\n";
214 0 : $content .= "URL=".$file->getLink();
215 :
216 0 : $suffix = 0;
217 0 : while ($this->archiver->locateName($filePath, ZipArchive::FL_NOCASE) != FALSE) {
218 0 : if ($suffix == 0) {
219 0 : $pattern = ".".$file->getExtension()."$";
220 0 : } else {
221 0 : $pattern = "-".$suffix.".".$file->getExtension()."$";
222 : }
223 0 : $suffix++;
224 0 : $replacement = "-".$suffix.".".$file->getExtension();
225 :
226 0 : $filePath = ereg_replace($pattern, $replacement, $filePath);
227 0 : }
228 :
229 0 : if (!$this->archiver->addFromString($filePath, $content)) {
230 0 : throw new ArchiverException("Datei '".$file->getName().
231 0 : "' konnte nicht zum Archiv hinzugefügt werden");
232 : }
233 :
234 0 : $this->addResourceDescription($file, $filePath);
235 0 : }
236 :
237 : /**
238 : * Adding resource description as comment to the archive.
239 : *
240 : * @param Resource $resource Resource
241 : * @param String $path Path to file in archive
242 : */
243 : private function addResourceDescription(Resource $resource, $path) {
244 :
245 0 : $description = $resource->getDescription();
246 : //$description = iconv ("windows-1251", "ASCII//TRANSLIT", $description);
247 0 : $description = iconv ("latin1", "ASCII//TRANSLIT", $description);
248 :
249 0 : if (!$this->archiver->setCommentName($path, $description)) {
250 0 : throw new ArchiverException("Für die Ressource '".$resource->getName().
251 0 : "' konnte kein Kommentar hinzugefügt werden");
252 : }
253 0 : }
254 : }
255 :
|